<!--
 * @description  : 市管-日计划终止
 * @descriptionDetail: 市管-日计划终止
 * @copyright    : 浙江烟草
 * @author       : sy
 * @create       : 2022-09-06 15:57:44
-->
<template>
  <view class="page">
    <!-- #ifdef H5-ONLY -->
    <theme-navbar :is-white="false" :title="navTitle" :custom-back="goBack" style="z-index: 99999"></theme-navbar>
    <!-- #endif -->
    <view class="content">
      <u-form :model="dataForm" :rules="rules" :errorType="errorType" ref="uForm">
        <u-form-item label="终止原因" :required="isTrue" prop="applyReasonName" label-position="top">
          <dic-input
            :keyWord="$u.dic.DIC_KEYS.M_DAY_PLAN_TERMINATION_REASON"
            :dicCode="dataForm.applyReason"
            type="select"
            :border="border"
            :select-open="dicSelectOpen($u.dic.DIC_KEYS.M_DAY_PLAN_TERMINATION_REASON)"
            placeholder="请选择终止原因"
            v-model="dataForm.applyReasonName"
            @changeInputModelData="
              (name) => {
                $set(dataForm, 'applyReasonName', name);
              }
            "
            @click="showDic($u.dic.DIC_KEYS.M_DAY_PLAN_TERMINATION_REASON, dataForm.applyReason)"
          />
        </u-form-item>   
        <u-form-item label="备注说明" :required="isTrue" prop="remark" label-position="top">
          <u-input type="textarea" height="100" :auto-height="true" placeholder="请输入" v-model="dataForm.remark" />
        </u-form-item>
      </u-form>
      <!-- 数据字典 -->
      <dic-choice :keyWord="dicKeyWords" :currentSelect="dicCurrentSelect" v-model="dicShow" @confirm="selectDicConfirm"></dic-choice>
    </view>
    <view class="submit-btns btn-other-cls">
      <u-button class="del" type="primary" @click="stopPlanBtn">
        保存 
      </u-button>
    </view>
  </view>
</template>

<script>
import DicChoice from "@/components/dic_choice.vue";
import DicInput from "@/components/dic_input.vue";
import checkService from "@/service/check/check.service";
export default {
  components: {
    DicChoice,
    DicInput,
  },
  data() {
    return {
      dicShow: false, // dic_select 是否显示
      dicKeyWords: "", // dic 关键字
      dicCurrentSelect: "", // dic 当前选择项
      dataForm: {}, 
      rules: {
        applyReasonName: [
          {
            required: true,
            message: "请选择终止原因",
            trigger: "change",
          },
        ],
        remark: [
          {
            required: true,
            message: "请输入备注说明",
            trigger: ["change", "blur",],
          },
        ],
      },
      errorType: ['border-bottom', 'message',],
      isTrue: true,
      border: false,
    };
  },
  onReady() {
    this.$refs.uForm.setRules(this.rules);
  },
  // 加载初始周期方法
  onLoad(opt) {
    this.dataForm.busUuid = opt.dayUuid;
  },
  methods: {
    // 数据字典组件选择事件
    dicSelectOpen: function(keyWords) {
      if (this.dicKeyWords == keyWords && this.dicShow) {
        return true;
      }
      return false;
    },
    showDic: function(key, cur) {
      if (this.dicShow) return;
      this.dicKeyWords = key;
      this.dicCurrentSelect = cur;
      this.dicShow = true;
    }, 
    selectDicConfirm(e) {
      if (this.dicKeyWords == this.$u.dic.DIC_KEYS.M_DAY_PLAN_TERMINATION_REASON) { // 终止原因
        this.dataForm.applyReasonName = e[0].label;
        this.dataForm.applyReason = e[0].extra.bcbCode;    
      }
    },
   
    // 日计划终止接口
    stopPlanBtn() {     
      this.$refs.uForm.validate((valid) => {
        if (valid) {
          this.toSelectPerson();
        } else {
          console.log("表单验证失败");
        }
      });
    },
    toSelectPerson(){
      this.$u.route({
        url: "/pages/commonPage/flowUserSelectComponent",
        params: {
          flowFormCode: "dayPlanTerminate", // 日计划终止的gfcode
          // docInsUuid: this.dataForm.busUuid,
        },
      });
    },
    updateBackState(param) {
      if (param.flowUserSelectBack) { // 选择审批人
        let nextPersons = [];
        for (let fu of param.flowUserSelectBack) {
          nextPersons.push(fu.personId);
        }
        this.stopSubmit(nextPersons.join(","));
      }
    },
    stopSubmit(persons){		
      this.$u.loading("日计划终止中...");
      let params = {
        ...this.dataForm,
        nextPersonUuids: persons || '',
        bizUuid:this.dataForm.busUuid,
        processDefKey:"dayPlanTerminate",
      }
      checkService
        .updateDayPlanToTerminte(params)
        .then((res) => {
          this.$u.hideLoading();
          if (res.success) {
            this.$u.toast(res.msg);
            // this.$u.route('/pages/check/checkList');
            this.backPrev({
              paramKey: "refresh",
              param : true, 
            });
          } else {
            this.$u.toast("终止失败！");
          }
        })
        .catch((err) => {
          this.$u.hideLoading();
          this.$u.toast(err);
        });
    },
  },
};
</script>

<style lang="scss" scoped>
.page{
	width: 100%;
	height: 100%;
	display: flex;
	flex-direction: column;
	padding: 0;
	.content {
      padding: 4rpx 34rpx;
	}
}
// 页面底部按钮样式
.submit-btns {
  display: flex;
  position: fixed;
  bottom: 40rpx;
  padding-left: 40rpx;
  padding-right: 40rpx;
  width: 100%;
  text-align: center;
  z-index: 999;
  .del {
    flex: 1;
  }
  .mit {
    flex: 1;
    margin-left: 40rpx;
  }
}
.btn-other-cls{
  padding-top: 20rpx;
  position: fixed;
  bottom: 0;
  padding-bottom: 20rpx;
  background-color: white;
  border-top: 1px solid $u-border-color;
}
</style>
